package cn.sciento.transfer.api.controller.v1;

import cn.sciento.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import cn.sciento.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import cn.sciento.core.base.BaseController;
import cn.sciento.core.util.Results;
import cn.sciento.mybatis.domian.SecurityToken;
import cn.sciento.mybatis.helper.SecurityTokenHelper;
import cn.sciento.starter.keyencrypt.core.Encrypt;
import cn.sciento.transfer.app.service.DbMigrationLogService;
import cn.sciento.transfer.domain.entity.DbMigrationLog;
import cn.sciento.transfer.domain.repository.DbMigrationLogRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@RestController("dbMigrationLogSiteController.v1")
@RequestMapping({"/v1/db-migration-logs"})
public class DbMigrationLogSiteController extends BaseController {
  private static final Logger LOGGER = LoggerFactory.getLogger(DbMigrationLogSiteController.class);
  
  @Autowired
  private DbMigrationLogRepository dbMigrationLogRepository;
  
  @Autowired
  private DbMigrationLogService dbMigrationLogService;
  
  @ApiOperation("表结构变更日志列表")
  @Permission(level = ResourceLevel.SITE)
  @GetMapping
  public ResponseEntity list(@Encrypt DbMigrationLog dbMigrationLog, @ApiIgnore @SortDefault(value = {"dbMigrationLogId"}, direction = Sort.Direction.DESC) PageRequest pageRequest) {
    return Results.success(this.dbMigrationLogRepository.pageQueryByOption(dbMigrationLog, pageRequest));
  }
  
  @ApiOperation("表结构变更日志明细")
  @Permission(level = ResourceLevel.SITE)
  @GetMapping({"/{dbMigrationLogId}"})
  public ResponseEntity<DbMigrationLog> detail(@Encrypt @PathVariable Long dbMigrationLogId) {
    DbMigrationLog dbMigrationLog = (DbMigrationLog)this.dbMigrationLogRepository.selectByPrimaryKey(dbMigrationLogId);
    return Results.success(dbMigrationLog);
  }
  
  @ApiOperation("创建表结构变更日志")
  @Permission(level = ResourceLevel.SITE)
  @PostMapping
  public ResponseEntity<DbMigrationLog> create(@Encrypt @RequestBody DbMigrationLog dbMigrationLog) {
    validObject(dbMigrationLog, new Class[0]);
    this.dbMigrationLogRepository.insertSelective(dbMigrationLog);
    return Results.success(dbMigrationLog);
  }
  
  @ApiOperation("修改表结构变更日志")
  @Permission(level = ResourceLevel.SITE)
  @PutMapping
  public ResponseEntity<DbMigrationLog> update(@Encrypt @RequestBody DbMigrationLog dbMigrationLog) {
    SecurityTokenHelper.validToken((SecurityToken)dbMigrationLog);
    this.dbMigrationLogRepository.updateByPrimaryKeySelective(dbMigrationLog);
    return Results.success(dbMigrationLog);
  }
  
  @ApiOperation("删除表结构变更日志")
  @Permission(level = ResourceLevel.SITE)
  @DeleteMapping
  public ResponseEntity<?> remove(@Encrypt @RequestBody DbMigrationLog dbMigrationLog) {
    SecurityTokenHelper.validToken((SecurityToken)dbMigrationLog);
    this.dbMigrationLogRepository.deleteByPrimaryKey(dbMigrationLog);
    return Results.success();
  }
  
  @ApiOperation("执行表结构变更")
  @Permission(level = ResourceLevel.SITE)
  @PostMapping({"/execute"})
  public ResponseEntity<?> execute(@Encrypt @RequestBody DbMigrationLog dbMigrationLog) {
    SecurityTokenHelper.validToken((SecurityToken)dbMigrationLog);
    this.dbMigrationLogService.doExecuteDbMigration(dbMigrationLog.getDbMigrationLogId());
    return Results.success();
  }
  
  @ApiOperation("表结构变更同步")
  @Permission(level = ResourceLevel.SITE)
  @PostMapping({"/sync"})
  public ResponseEntity<?> sync() {
    List<DbMigrationLog> dbMigrationLogList = this.dbMigrationLogService.probeDbMigrations();
    dbMigrationLogList.forEach(dbMigrationLog -> {
          String updateDetail = this.dbMigrationLogService.doExecuteDbMigration(dbMigrationLog.getDbMigrationLogId());
          LOGGER.info("=================[{}]================", updateDetail);
        });
    return Results.success();
  }
}
